<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.min.css" rel="stylesheet" />
		<link href="css/mui.indexedlist.css" rel="stylesheet" />
		
		<style>
			html,
			body {
				height: 100%;
				overflow: hidden;
			}
			.mui-bar {
				-webkit-box-shadow: none;
				box-shadow: none;
			}
		</style>
	</head>

	<body>
		<div class="mui-content">
			
			<div id='list' class="mui-indexed-list">
				<div class="mui-indexed-list-search mui-input-row mui-search">
					<input type="search" class="mui-input-clear mui-indexed-list-search-input" placeholder="搜索好友">
				</div>
				<div class="mui-indexed-list-bar">
					<a>A</a>
					<a>B</a>
					<a>C</a>
					<a>D</a>
					<a>E</a>
					<a>F</a>
					<a>G</a>
					<a>H</a>
					<a>I</a>
					<a>J</a>
					<a>K</a>
					<a>L</a>
					<a>M</a>
					<a>N</a>
					<a>O</a>
					<a>P</a>
					<a>Q</a>
					<a>R</a>
					<a>S</a>
					<a>T</a>
					<a>U</a>
					<a>V</a>
					<a>W</a>
					<a>X</a>
					<a>Y</a>
					<a>Z</a>
					<a>#</a>
				</div>
				<div class="mui-indexed-list-alert"></div>
				<div class="mui-indexed-list-inner">
					<div class="mui-indexed-list-empty-alert">没有数据</div>
					<ul id="contactList" class="mui-table-view">
					</ul>
				</div>
			</div>
		</div>
	
		
		<script src="js/mui.min.js"></script>
		<script src="js/mui.indexedlist.js"></script>
		<script src="js/nickname.js"></script>
		<script src="js/app.js"></script>
		<script type="text/javascript">
			mui.init();
			mui.plusReady(function() {
				// 监听当前webview的show事件
				var thisWebview = plus.webview.currentWebview();
				thisWebview.addEventListener("show", function(){
					// 先渲染一次
					renderContactList();
					// 查询服务器再次渲染一次
					showList();
				});
				window.addEventListener("refresh",function(){
					renderContactList();
				});
				window.addEventListener("refriends",function(){
					getContactList();
				});
			});
			
			// 26个字母外加 # 号
			var enWords = [
				'A', 'B', 'C', 'D', 'E', 'F', 'G', 
				'H', 'I', 'J', 'K', 'L', 'M', 'N',
				'O', 'P', 'Q', 'R', 'S', 'T', 
				'U', 'V', 'W', 'X', 'Y', 'Z',
				'#'
			]
			// 构建通讯录二维数组模型
			var contactArray = [
				[],[],[],[],[],[],[],[],[],
				[],[],[],[],[],[],[],[],[],
				[],[],[],[],[],[],[],[],[]
			]
			// 清空通讯录模型数组
			function clearContactArray() {
				contactArray = [
					[],[],[],[],[],[],[],[],[],
					[],[],[],[],[],[],[],[],[],
					[],[],[],[],[],[],[],[],[]
				]
			}
			// 获取英文字母所在数组的位置
			function getOrderOfEnWords(enChar) {
				for (var i = 0 ; i < enWords.length ; i ++) {
					if (enWords[i] == enChar) {
						return i;
					}
				}
				// 如果没有对应的值，说明用户昵称的首字母是乱七八糟的字符或者数字或者表情
				return enWords.length - 1;
			}
			function renderContactList(){
				var friendList = app.getContactList();
				// 把所有好友转为拼音分组的二维数组
				let friendsLength = friendList.length;
				for (var i=0;i<friendsLength;i++) {
					var friend = friendList[i];
					// 名字转拼音
					var pingyin = words.convertPinyin(friend.friendNickName)
					// 2.2 截取拼音的首字母转大写
					var firstChar = pingyin.substr(0, 1).toUpperCase();
					// 2.3 获取首字母在二维数组中的位置
					var order = getOrderOfEnWords(firstChar);
					// 2.4 获得顺序之后，塞入朋友
					contactArray[order].push(friend);
				}
				if(friendsLength<1){
					return;
				}
				// 渲染html界面
				var contactHtml = "";
				for (var i = 0 ; i < contactArray.length ; i ++ ) {
					var friendArray = contactArray[i];
					if (friendArray.length > 0) {
						// 首字母
						var nicknameStarter = enWords[i];
						contactHtml += '<li data-group="'+nicknameStarter+'" class="mui-table-view-divider mui-indexed-list-group">'+nicknameStarter+'</li>'
						for (var j = 0 ; j < friendArray.length ; j ++ ) {
							
							contactHtml += '<li friendUserId="' + friendArray[j].friendUserId + '" friendNickName="' + friendArray[j].friendNickName + '" friendFaceImg="' + friendArray[j].friendFaceImg + '"class="chat-with-friend mui-media mui-table-view-cell mui-indexed-list-item" style="padding: 8px 15px; ">' +
												'<div class="mui-slider-left mui-disabled" >' + 
													'<a class="mui-btn mui-btn-danger mui-icon"  style="font-size: 15px;line-height: 35px;">删除</a>' +
												'</div>'+
												'<div class="mui-slider-handle">'+
													'<img class="mui-media-object mui-pull-left" style="max-height: 35px;max-width: 35px;" src="'+friendArray[j].friendFaceImg+'"/>'+
													'<div class="mui-media-body" style="line-height: 35px;">'+friendArray[j].friendNickName+'</div>'+
												'</div>'+
											'</li>'
						}
					}
				}
				// 渲染html
				document.getElementById("contactList").innerHTML = contactHtml;
				// 清空数组
				clearContactArray();
				
				mui("#contactList").on("tap",".chat-with-friend",function(){
					let me = app.getUserInfo();
					// 打开聊天页面
					mui.openWindow({
						url: "chating/chatting.html",
						id: "chatting-" + this.getAttribute("friendUserId"),
						extras:{
							friendUserId : this.getAttribute("friendUserId"),
							friendNickName: this.getAttribute("friendNickName"),
							friendFaceImg: this.getAttribute("friendFaceImg")
						}
					})
					// 标记未读状态为已读
					app.readUserChatSnapshot(me.userId ,this.getAttribute("friendUserId"));
					// 重新拉取好友
					let myChatListWebview = plus.webview.getWebviewById("chatlist.html");
					mui.fire(myChatListWebview, "refresh");
				});
			}
			
			// 显示通讯录
			function showList() {
				var list = document.getElementById('list');
				list.style.height = document.body.offsetHeight + "px";
				// 创建列表显示
				window.indexedList = new mui.IndexedList(list);
				getContactList();
			}
			// 获取联系人列表
			function getContactList(){
				var _user_info = app.getUserInfo();
				mui.ajax(app.serverUrl + "/f/queryMyFriends?userId="+_user_info.userId,{
					dataType:'json',//服务器返回json格式数据
					type:'post',//HTTP请求类型
					timeout:10000,//超时时间设置为10秒；
					headers:{'Content-Type':'application/json'},	              
					success:function(data){
						if(data.status == 200){
							app.setContactList(data.data);
							renderContactList();
						}else{
							app.showToast(data.msg, "error");	
						}
					},
					error:function(xhr,type,errorThrown){
						
					}
				});
			}
		</script>
	</body>

</html>